//
//  CENoteEditorControllerViewController.m
//  TextKitNotepad
//
//  Created by Colin Eberhardt on 19/06/2013.
//  Copyright (c) 2013 Colin Eberhardt. All rights reserved.
//

#import "NoteEditorViewController.h"
#import "TimeIndicatorView.h"
#import "Note.h"

@interface NoteEditorViewController () <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation NoteEditorViewController
{
    TimeIndicatorView* _timeView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.textView.text = self.note.contents;
    self.textView.delegate = self;
    //动态文本的基本支持
    self.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    //回应用户字体变化设置
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
    
    _timeView = [[TimeIndicatorView alloc] init:_note.timestamp];
    [self.view addSubview:_timeView];
}

- (void)preferredContentSizeChanged:(NSNotification *)notification
{
    //根据新的字体设置来设定textView中的字体
    self.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    [self updateTimeIndicatorFrame];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    // copy the updated note text to the underlying model.
    _note.contents = textView.text;
}

- (void)viewDidLayoutSubviews
{
    [self updateTimeIndicatorFrame];
}

- (void)updateTimeIndicatorFrame
{
    //调用updateSize来设定各个subViews的尺寸
    [_timeView updateSize];
    //将这些subViews放在右上角
    _timeView.frame = CGRectOffset(_timeView.frame, self.view.frame.size.width - _timeView.frame.size.width, 0.0);
    
    //环绕路径
    UIBezierPath *exclusionPath = [_timeView curvePathWithOrigin:_timeView.center];
    _textView.textContainer.exclusionPaths = @[exclusionPath];
}
@end
